//
//  WJPerson.m
//  JerryIos
//
//  Created by jerrywang on 2018/2/4.
//  Copyright © 2018年 王军. All rights reserved.
//

#import "WJPerson.h"

@implementation WJPerson

+(instancetype) personWithDict:(NSDictionary *)dict {
    return [[WJPerson alloc] initWithDict:dict];
}

-(instancetype) initWithDict:(NSDictionary *)dict {
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}


/**
 * 计算文字尺寸
 * 第一个参数：需要计算尺寸文字
 * 第二个参数：文字的字体
 * 第三个参数：文字最大尺寸
 */
-(CGSize) sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize {
    NSDictionary *attrs = @{NSFontAttributeName:font};
    return [text boundingRectWithSize:maxSize
                              options:NSStringDrawingUsesLineFragmentOrigin
                           attributes:attrs
                              context:nil].size;
}

-(void) calculateFrame {
    
    CGFloat padding = 10;
    
    CGFloat iconX = padding;
    CGFloat iconY = padding;
    CGFloat iconW = 30;
    CGFloat iconH = 30;
    _iconFrame = CGRectMake(iconX, iconY, iconW, iconH);
    
    CGSize nameSize = [self sizeWithText:_name font:WJNameFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    CGFloat nameX = CGRectGetMaxX(_iconFrame) + padding;
    CGFloat nameY = iconY + (iconH - nameSize.height) * 0.5;
    _nameFrame = CGRectMake(nameX, nameY, nameSize.width, nameSize.height);
    
    CGFloat vipX = CGRectGetMaxX(_nameFrame) + padding;
    CGFloat vipY = nameY;
    CGFloat vipW = 15;
    CGFloat vipH = 15;
    _vipFrame = CGRectMake(vipX, vipY, vipW, vipH);
    
    CGFloat textX = iconX;
    CGFloat textY = CGRectGetMaxY(_iconFrame) + padding;
    CGSize textSize = [self sizeWithText:_text font:WJTextFont maxSize:CGSizeMake(300, MAXFLOAT)];
    _textFrame = CGRectMake(textX, textY, textSize.width, textSize.height);
    
    if (_pic) {
        CGFloat picX = textX;
        CGFloat picY = CGRectGetMaxY(_textFrame) + padding;
        CGFloat picW = 100;
        CGFloat picH = 100;
        _picFrame = CGRectMake(picX, picY, picW, picH);
        
        _cellHeight = CGRectGetMaxY(_picFrame) + padding;
    } else {
        _cellHeight = CGRectGetMaxY(_textFrame) + padding;
    }
    
}

@end
